

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Try not to become a man of success but rather try to become a man of value. ">
  <meta name="author" content="kinzun">
  <meta name="keywords" content>
  <meta name="description" content="Python2&amp;amp;3 编码区别Python2.X中​    编码格式默认 ASCII，字符串存在两种格式， str ,unicode 因为受系统的影响所以，Python 2.x在头部加上一行编码声明。 编码的转换,需要转换为unicode为中间, s.decode(&amp;#39;utf-8&amp;#39;) 受系统 Win GB2312 Linux 默认UTF-8 Python3.X中编码默认unic">
<meta name="keywords" content="Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python编码，文件编码的操作">
<meta property="og:url" content="https://kinzun.github.io/2018/09/12/字符编码（character encoding,fileoperate/index.html">
<meta property="og:site_name" content="kinzun">
<meta property="og:description" content="Python2&amp;amp;3 编码区别Python2.X中​    编码格式默认 ASCII，字符串存在两种格式， str ,unicode 因为受系统的影响所以，Python 2.x在头部加上一行编码声明。 编码的转换,需要转换为unicode为中间, s.decode(&amp;#39;utf-8&amp;#39;) 受系统 Win GB2312 Linux 默认UTF-8 Python3.X中编码默认unic">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-25T08:52:46.940Z">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Python编码，文件编码的操作">
<meta name="twitter:description" content="Python2&amp;amp;3 编码区别Python2.X中​    编码格式默认 ASCII，字符串存在两种格式， str ,unicode 因为受系统的影响所以，Python 2.x在头部加上一行编码声明。 编码的转换,需要转换为unicode为中间, s.decode(&amp;#39;utf-8&amp;#39;) 受系统 Win GB2312 Linux 默认UTF-8 Python3.X中编码默认unic">
  
  <title>Python编码，文件编码的操作 - kinzun</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css">
  <link rel="stylesheet" href="/lib/hint/hint.min.css">

  
    
    
      
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css">
    
  

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">

<link rel="stylesheet" href="/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"kinzun.github.io","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script src="/js/utils.js"></script>
  <script src="/js/color-schema.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>kinzun</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Python编码，文件编码的操作">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2018-09-12 16:30" pubdate>
        2018年9月12日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      6 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Python编码，文件编码的操作</h1>
            
            <div class="markdown-body">
              <h1 id="Python2-amp-3-编码区别"><a href="#Python2-amp-3-编码区别" class="headerlink" title="Python2&amp;3 编码区别"></a>Python2&amp;3 编码区别</h1><h5 id="Python2-X中"><a href="#Python2-X中" class="headerlink" title="Python2.X中"></a>Python2.X中</h5><p>​    编码格式默认 <code>ASCII</code>，字符串存在两种格式， <code>str</code> ,<code>unicode</code></p>
<p>因为受系统的影响所以，Python 2.x在头部加上一行编码声明。</p>
<p>编码的转换,需要转换为<code>unicode</code>为中间,</p>
<p><code>s.decode(&#39;utf-8&#39;)</code></p>
<p>受系统</p>
<p>Win GB2312</p>
<p>Linux 默认UTF-8</p>
<h5 id="Python3-X中"><a href="#Python3-X中" class="headerlink" title="Python3.X中"></a>Python3.X中</h5><p>编码默认<code>unicode</code>，python3中默认<code>unicode</code>所以字符串被直接处理作为中文，在python2有的字节串是展示不出来的。</p>
<p> 不受系统的影响统一为unicode，py文件默认为UTF-8可以不用声明</p>
<h5 id="判断编码"><a href="#判断编码" class="headerlink" title="判断编码"></a>判断编码</h5><p>1、使用isinstance(s, str)来判断一个字符串是否为一般字符串（str为ascii类型的字符串，utf-8、utf-16、GB2312、GBK等都是ascii类型的字符串）；</p>
<p>2、使用type()或者.<strong>class</strong></p>
<h5 id="查看文件"><a href="#查看文件" class="headerlink" title="查看文件"></a>查看文件</h5><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> chardet<br><br> data =open(<span class="hljs-string">u"ff.txt"</span>, <span class="hljs-string">"rb"</span>).read() <br><br> chardet.detect(data) <br><br>&#123;<span class="hljs-string">'encoding'</span>: <span class="hljs-string">'GB2312'</span>, <span class="hljs-string">'confidence'</span>: <span class="hljs-number">0.99</span>, <span class="hljs-string">'language'</span>: <span class="hljs-string">'Chinese'</span>&#125;<br></code></pre></div></td></tr></table></figure>
<h4 id="字符编码（character-encoding）"><a href="#字符编码（character-encoding）" class="headerlink" title="字符编码（character encoding）"></a>字符编码（<strong>character encodin</strong>g）</h4><p>8 bit = 1Byte(字节)</p>
<p>编码:</p>
<ul>
<li><p>ASCII.（American Standard Code for Information Interchange，汉字内码扩展规范）</p>
<p>8bit. 1Byte</p>
</li>
<li><p>GBK.（汉字内码扩展规范） 国标码. 2Byte +&gt;&gt; 1字符</p>
<p>GB2312（中国国家标准简体中文字符集） 16bit, 2byte 两个字节表示一个字符</p>
<p>GB18030 与GBK与GB2312不同的是，GB18030采用单字节、双字节和四字节三种方式对字符编码</p>
<p>GB2312 &lt; GBK &lt; GB18030</p>
</li>
<li><p>Unicode. 万国码. 32bit, 4byte ()</p>
</li>
<li>UTF-8. 可变长度的Unicode.<br>英文: 1byte<br>欧洲: 2byte<br>中文: 3byte</li>
<li>编码之后的数据bytes<br>bytes.decode() 解码</li>
</ul>
<h5 id="编码"><a href="#编码" class="headerlink" title="编码"></a>编码</h5><ul>
<li>encode: 将默认编码（unicode)转换为其他编码。</li>
<li>decode: 将默认或者指定编码字符串转换为 unicode。</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"> s = <span class="hljs-string">"he"</span> ; s<br>    <br>  <br>s.decode(); u <span class="hljs-comment"># UTF-8，gbk  -&gt; UNICODE </span><br><br>gb = s.encode(<span class="hljs-string">"gb2312"</span>);gb<span class="hljs-comment"># UTF-8 -&gt; GB2312</span><br>gb.decode(<span class="hljs-string">"gb2312"</span>)<span class="hljs-comment"># 可以将其转换成 UNICODE。</span><br><br>s.encode <span class="hljs-comment"># UNICODE -&gt; utf-8或gbk</span><br><span class="hljs-string">u'\u4e2d\u56fd\u4eba'</span><br>gb.decode(<span class="hljs-string">"gb2312"</span>).encode() <span class="hljs-comment">#然后再转换成 UTF-8</span><br></code></pre></div></td></tr></table></figure>
<p><a href="http://python.jobbole.com/88967/" target="_blank" rel="noopener">更多</a></p>
<h4 id="文件的操作"><a href="#文件的操作" class="headerlink" title="文件的操作"></a>文件的操作</h4><figure class="highlight livecodeserver"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs undefined">基于对位置的<br><br><span class="hljs-built_in">seek</span><br><br><span class="hljs-number">0</span>，以文件开头，不限定打开方式。其它的两种限定要以b模式打开<br><br><span class="hljs-number">1</span>， 相对于上次光标停留的位置<br><br><span class="hljs-number">2</span>，从文件未尾，倒序。位置要用负数表示<br>       文件操作<br>            <span class="hljs-built_in">open</span>(文件路径, mode=<span class="hljs-string">"模式"</span>, encoding=<span class="hljs-string">"编码"</span>)<br>            模式: r, w, <span class="hljs-keyword">a</span>, r+, w+, <span class="hljs-keyword">a</span>+, rb, wb, ab<br>            <span class="hljs-keyword">for</span>循环可以迭代文件句柄. 拿到的是一行一行的内容<br>            <span class="hljs-built_in">seek</span>()移动光标<br>            tell()查看光标位置<br><br>            <span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(xxxx) <span class="hljs-keyword">as</span> f:<br>                操作<br><br>            文件修改:<br>               从源文件中读取内容. 修改, 然后写入到新文件中. 删除源文件. 重命名新文件<br></code></pre></div></td></tr></table></figure>
<p>1B bytes (字节) = 8bit </p>
<p>Utf-8 中，中文1 字符 = 3 字节</p>
<p>seek（3）*<em>UTF-8中 <code>一个中文 3 个字节</code></em> 读取<code>1个字符</code></p>
<p>read(n) 读取<code>n个字符</code>，rb模式则读取的是<code>n个字节</code></p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">with</span> open(<span class="hljs-string">'te.txt'</span>,<span class="hljs-string">'r'</span>) <span class="hljs-keyword">as</span> f1:<br>    connet = f1.read(<span class="hljs-number">1</span>) <span class="hljs-comment">#读取1个字节</span><br>    <span class="hljs-comment">#read(n) 读取n个字符，rb模式则读取的是n个字节</span><br>    f1.seek(<span class="hljs-number">3</span>) <span class="hljs-comment">#一个中文字三个字节</span><br>    conn2 = f1.read()<br>    print(connet)<br>    print(conn2)<br></code></pre></div></td></tr></table></figure>
<ol>
<li><p>seek(n) 光标移动到n位置, 注意, <strong>移动的单位是byte. 所以如果是UTF-8的中⽂文部分要</strong><br><strong>是3的倍数.</strong></p>
</li>
<li><p>通常我们使⽤用seek都是移动到开头或者结尾.<br> 移动到开头: seek(0)<br> 移动到结尾: seek(0,2) seek的第⼆二个参数表⽰示的是从哪个位置进⾏行行偏移, <strong>默认是0, 表</strong> </p>
<p><strong>⽰示开头, 1表⽰示当前位置, 2表⽰示结尾</strong> </p>
</li>
</ol>
<p><a href="https://www.douban.com/note/651149148/" target="_blank" rel="noopener">一字符</a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Python/">Python</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2018/09/17/深浅拷贝/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Python 深浅拷贝</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2018/05/20/Docker_cheat_sheet/">
                        <span class="hidden-mobile">Docker Cheat Sheet</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
